﻿using System;
using System.Data;
using System.Windows.Forms;
using Util.Database;
using System.Globalization;

namespace ConfigureNZBLoader.EditControls
{
    public partial class DownloadRequest : BaseEditControl
    {
        private long _Episode;
        public long Episode
        {
            get { return _Episode; }
        }

        private long _Season;
        public long Season
        {
            get { return _Season; }
        }

        private long _Id;
        public long Id
        {
            get { return _Id; }
        }


        public DownloadRequest()
        {
            InitializeComponent();
        }

        public void Open(long id)
        {
            _Id = id;
            using (IDbCommand cmd = DB.CreateCommand())
            {
                cmd.CommandText = "select * from request where id=?";
                IDataParameter par = cmd.CreateParameter();
                par.Value = id;
                cmd.Parameters.Add(par);

                using (IDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        _Season = (long)dr["SEASON"];
                        _Episode = (long)dr["EPISODE"];
                    }
                    else
                    {
                        throw new ArgumentException("Invalid ID", "id");
                    }
                }
            }

            if (Season == 0 && Episode == 0)
            {
                rdAll.Checked = true;
            }
            else if (Episode == 0)
            {
                rdSeason.Checked = true;
                txtSeason2.Text = Season.ToString(CultureInfo.InvariantCulture);
            }
            else
            {
                rdSingle.Checked = true;
                txtSeason.Text = Season.ToString(CultureInfo.InvariantCulture);
                txtEpisode.Text = Episode.ToString(CultureInfo.InvariantCulture);
            }
        }


        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string str;
            if (rdAll.Checked)
            {
                _Season = 0;
                _Episode = 0;
                str = "All seasons/episodes";
            }
            else if (rdSeason.Checked)
            {
                if (!long.TryParse(txtSeason2.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out _Season))
                {
                    MessageBox.Show("Invalid value for Season", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                    return;
                }
                _Episode = 0;
                str = string.Format(CultureInfo.InvariantCulture, "Season {0}", _Season);
            }
            else
            {
                if (!long.TryParse(txtSeason.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out _Season))
                {
                    MessageBox.Show("Invalid value for Season", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                    return;
                }
                if (!long.TryParse(txtEpisode.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out _Episode))
                {
                    MessageBox.Show("Invalid value for Episode", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
                    return;
                }
                str = string.Format(CultureInfo.InvariantCulture, "Season {0} Episode {1}", _Season, _Episode);
            }

            using (IDbCommand cmd = DB.CreateCommand())
            {
                cmd.CommandText = "update request set season=?, episode=? where id=?";
                IDataParameter par = cmd.CreateParameter();
                par.Value = Season;
                cmd.Parameters.Add(par);
                par = cmd.CreateParameter();
                par.Value = Episode;
                cmd.Parameters.Add(par);
                par = cmd.CreateParameter();
                par.Value = Id;
                cmd.Parameters.Add(par);

                cmd.ExecuteNonQuery();
            }

            ItemChangedNotification(str);
        }


        private void txtSeason_TextChanged(object sender, EventArgs e)
        {
            rdSingle.Checked = true;
        }


        private void txtSeason2_TextChanged(object sender, EventArgs e)
        {
            rdSeason.Checked = true;
        }
    }
}
